package com.java.shop.ware.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

import static com.java.common.constant.MqConstant.*;

@Configuration
public class MyRabbitmqConfig {


    /**
     * 序列化
     * @return
     */
    @Bean
    public MessageConverter setMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public Exchange stockEventExchange(){
        return new TopicExchange(ware_exchangeName,true,false);
    }

    @Bean
    public Queue stockDelayQueue(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange",ware_exchangeName);
        map.put("x-dead-letter-routing-key","stock.release");
        map.put("x-message-ttl",ware_eeadQueueTTL);
        return new Queue(ware_deadQueue,true,false,false,map);
    }

    @Bean
    public Queue stockReleaseStockQueue(){
        return new Queue(ware_ordQueue,true,false,false);
    }

    @Bean
    public Binding toDeadBinding(){
        return new Binding(ware_deadQueue, Binding.DestinationType.QUEUE,ware_exchangeName,ware_toDeadRoutingKey,null);
    }

    @Bean
    public Binding toOrdBinding(){
        return new Binding(ware_ordQueue, Binding.DestinationType.QUEUE,ware_exchangeName,ware_toOrdRoutingKey,null);
    }
}
